package com.weiyuwqj.controller.user;

import com.weiyuwqj.api.service.BusInvestService;
import com.weiyuwqj.api.service.BusItemAutoService;
import com.weiyuwqj.api.service.BusItemAutoSortService;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Constants;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.BusItemAuto;
import com.weiyuwqj.api.model.BusItemAutoSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangyong on 2016/11/25.
 * 自动投标
 */
@Controller
public class UserAutoController {

    @Autowired
    BusItemAutoService itemAutoService;

    @Autowired
    BusItemAutoSortService itemAutoSortService;

    @Autowired
    BusInvestService investService;

    /**
     * 自动投标页面
     * @return
     */
    @RequestMapping("/user/auto")
    public Object auto(){
        return "main/user/automatic";
    }

    /**
     * 自动投标页面信息回显
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/user/autoInfo", method= RequestMethod.POST)
    public Object autoInfo(HttpServletRequest request){
        /*Integer userId=(Integer)request.getSession().getAttribute("userId");
        //查询自动投标规则

        BusItemAuto itemAuto=itemAutoService.getAutoByUserId(userId);
        Integer autoSort = 0;//规则排序
        BigDecimal investAmount = BigDecimal.ZERO;//累计投资金额
        if(itemAuto != null){
            //根据规则id查询排序信息
            BusItemAutoSort itemAutoSort = itemAutoSortService.getAutoSortByAutoId(itemAuto.getId());
            //查询排行榜,如果规则是关闭的，则没有排序，默认给前段传排序0
            if(itemAutoSort == null){
                autoSort = 0;
            }else {
                autoSort = itemAutoSortService.countAutoNumById(itemAutoSort.getId());
            }
            //查询在投金额
            investAmount = investService.sumInvestAutoAmountByAutoIdAndType(itemAuto.getId());
        }
        Map map =new HashMap();
        map.put("investAmount",investAmount);
        map.put("itemAuto",itemAuto);
        map.put("autoSort",autoSort);
        return map;*/
        return null;
    }

    /**
     * 添加/修改 自动投标规则
     * @param auto
     * @param transactionPassword       交易密码
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/user/autoAdd", method= RequestMethod.POST)
    public Object autoAdd(BusItemAuto auto,String transactionPassword,HttpServletRequest request){
        Integer userId=(Integer)request.getSession().getAttribute("userId");
       // itemAutoService.insertOrModifyAuto(auto,transactionPassword,userId);
        return Message.OK;
    }

    //查询
    @ResponseBody
    @RequestMapping(value="/user/getAuto", method= RequestMethod.POST)
    public Object getOne(Paginator p,HttpServletRequest request){
        Integer userId=(Integer)request.getSession().getAttribute("userId");
        Map map = new HashMap();
        map.put("userId",userId);
        map.put("isShowDelete",0);
        //PageList auto=itemAutoService.getList(p,map);
        return null;
    }



//    //修改
//    @ResponseBody
//    @RequestMapping(value="/user/modifyAuto", method= RequestMethod.POST)
//    public Object modifyAuto(BusItemAuto auto,String inputCode,HttpServletRequest request){
//
//        if(auto.getItemDayMin().compareTo(auto.getItemDayMax())==1 || auto.getItemRateMin().compareTo(auto.getItemRateMax())==1){
//            throw new CrowdException("auto.min_not_max");
//        }
//
//        Integer userId=(Integer)request.getSession().getAttribute("userId");
//        auto.setItemUserId(userId);
//
//        Long validPhoneExpiredTime = (Long) request.getSession().getAttribute("validPhoneExpiredTime");
//        String validPhoneCode = request.getSession().getAttribute("validPhoneCode").toString();
//
//        if(auto.getItemStatus()==0){
//            BusItemAutoSort sort=itemAutoSortService.getAutoSortByAutoId(auto.getId());
//            if(sort!=null){
//                itemAutoSortService.delete(sort.getId());
//            }
//            itemAutoService.updateNotNull(auto);
//        }else {
//            //发送短信
//            Date now = new Date();
//            long between = (now.getTime() - validPhoneExpiredTime) / 1000;
//            if (!(validPhoneCode.equals(inputCode))) {
//                throw new CrowdException("user.register.randomCode.error");
//            }
//            if (between > Constants.VALIDATETIME) {
//                throw new CrowdException("user.register.randomCode.timeout");
//            }
//            BusItemAutoSort sort=itemAutoSortService.getAutoSortByAutoId(auto.getId());
//            if(sort!=null){
//                itemAutoSortService.delete(sort.getId());
//            }
//            sort=new BusItemAutoSort();
//            if(auto.getItemLockStatus()==0){
//                sort.setAutoType(2);
//            }else {
//                sort.setAutoType(1);
//            }
//            sort.setAutoId(auto.getId());
//            sort.setUserId(auto.getItemUserId());
//            itemAutoSortService.save(sort);
//            itemAutoService.updateNotNull(auto);
//        }
//        request.getSession().removeAttribute("validPhoneExpiredTime");
//        request.getSession().removeAttribute("validPhoneCode");
//        return Message.OK;
//
//    }

//    /**
//     * 逻辑删除
//     * @param autoId 规则id
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value="/user/deleteAuto", method= RequestMethod.POST)
//    public Object deleteAuto(Integer autoId){
//        itemAutoService.deleteAutoByAutoId(autoId);
//        itemAutoSortService.deleteByAutoId(autoId);
//        return Message.OK;
//    }

    //自动投标列表
    @ResponseBody
    @RequestMapping(value="/user/getInvestAuto", method= RequestMethod.POST)
    public Object getInvestAuto(Paginator p,HttpServletRequest request){

        Integer userId=(Integer)request.getSession().getAttribute("userId");

        Map map=new HashMap();
        map.put("userId",userId);
        map.put("investType",5);
        return investService.getListByUserIdAndType(p,map);
    }

    //修改自动投标状态
    @ResponseBody
    @RequestMapping(value="/user/modifyStatus", method= RequestMethod.POST)
    public Object modifyStatus(Integer id){
       // itemAutoService.modifyItemStatusById(0,1,id);
        return Message.OK;
    }



}
